<template>
	<view>
		<uni-nav-bar background-color="#3286ed" :statusBar="true" :fixed='true' color="#fff" title="新增仓库" left-icon="left" right-text="提交" @clickLeft="backFn" @clickRight="btnOkFn">  </uni-nav-bar>
	
	    <u--form class="form" labelPosition="left" labelWidth="100px" :model="form" :rules="rules" ref="form">
	    			
	    			<u-form-item   label="仓库名" prop="orderNo" borderBottom ref="item1">
	    			 	<u--input v-model="form.warehouseName" border="none" placeholder="请输入仓库名"></u--input>
	    			 </u-form-item>
					 <u-form-item   label="仓库编号" prop="warehouseNo" borderBottom ref="item1">
					  	<u--input v-model="form.warehouseNo" border="none" placeholder="仓库编号"></u--input>
					 	<!-- <u-icon slot="right" name="arrow-right"></u-icon> -->
					  </u-form-item>
					  <u-form-item   label="所属地区" prop="region" borderBottom ref="item1" @click="regionShow=true">
					   	<u--input :readonly="true" v-model="form.region" border="none" placeholder="所属地区"></u--input>
					  	<u-icon slot="right" name="arrow-right" @click="regionShow=true"></u-icon>
					   </u-form-item>
					   <u-form-item @click="adressFn"  label="仓库地址" prop="address" borderBottom ref="item1">
					    	<u--input v-model="form.address" border="none" placeholder="仓库地址"></u--input>
					   	<u-icon slot="right" name="arrow-right"></u-icon>
					    </u-form-item>
						<u-form-item   label="负责部门" prop="responsibleDeptName" borderBottom ref="item1">
						 	<u--input v-model="form.responsibleDeptName" border="none" placeholder="负责部门"></u--input>
							<u-icon slot="right" name="arrow-right"></u-icon>
						 </u-form-item>
						 <u-form-item @click="selectUser" label="负责人" prop="responsibleUserName" borderBottom ref="item1">
						  	<u--input v-model="form.responsibleUserName" border="none" placeholder="负责人"></u--input>
						 	<u-icon slot="right" name="arrow-right"></u-icon>
						  </u-form-item>
						  <u-form-item   label="备注" prop="remark" borderBottom ref="item1">
								<!-- <u--input v-model="form.remark" border="none" placeholder="备注"></u--input> -->
								<u--textarea v-model="form.remark" placeholder="请输入备注" ></u--textarea>
						   </u-form-item>
	    </u--form>
		
		<!-- 所属地区 -->
		<!-- " -->
		<u-picker  :closeOnClickOverlay="true" @close="regionShow=false"  @cancel="regionShow=false" @confirm="customerRegion":show="regionShow"  :columns="regionOptions" keyName="text" ></u-picker>
		<!-- 选择地址 -->
		<uni-data-picker class="picker" v-if="show==2" v-model="form.address" :localdata="addressList"
		 popup-title="选择地址"@change="onchange"   ref="adress"></uni-data-picker>
	</view>
</template>

<script>
	 import {provinceNameList} from '@/api/region/region.js';
	 import {deptTreeSelect} from '@/api/system/user.js'
	export default {
		data() {
			return {
				form:{
					        pageNum: 1,
					        pageSize: 10,
					        warehouseName: '',
					        warehouseNo: '',
					        regionId: '',
					        region: '',
					        address: '',
					        responsibleDept: '',
					        responsibleDeptName: '',
					        responsibleUser: '',
					        responsibleUserName: '',
					        createId: '',
					        status: ''
				},
				show:0,
				regionShow:false,//所属区域
				rules:{},
				adresslistDou:'',
				addressList:[],
				regionOptions:[]
			};
		},
		onLoad() {
			this.getNo() 
			this.getAddressList()
			this.getDept()
		},
		onShow(){
			var that =this
			uni.$on('updateData',function(data){
				console.log(data);
				that.form.responsibleUser=data.userId
				that.form.responsibleUserName=data.userName
			})
		},
		methods:{
			// 获取部门
			getDept(){
				deptTreeSelect().then(response=>{
					console.log(response);
				})
			},
			
			
			
			// 获取地址
			async getAddressList(){
				let res=await provinceNameList()
				console.log(res);
				this.regionOptions.push(res.rows)
				this.addressList= await this.getList(res.rows)
			},
			adressFn(){
				this.show=2
				this.$nextTick(()=>{
					this.$refs.adress.show()
				})
			},
			getList(data){
				data.forEach((item)=>{
					// console.log(item);
					if(item.label){
						item.text=item.label
						delete item.label
					}
					if(item.children){
						this.getList(item.children)
					}
				})
				 return data
			},
			async onchange(e){
						   // console.log(e);
						   this.form.loadingAddressAre=e.detail.value[2].text
						   this.form.loadingAddressCity=e.detail.value[1].text
						   this.form.loadingAddressProvince=e.detail.value[0].text
				let arr=[]
				e.detail.value.forEach((value)=>{
					arr.push(value.text)
				})
				this.form.address=arr.join('')
			 this.show=false
			},
			getNo(){
				this.form.warehouseNo=this.getWarehouseNo()
				console.log('11');
			},
			// 生成仓库编号
			getWarehouseNo (){
			      var date = new Date();
			      var year = date.getFullYear();        //年 ,从 Date 对象以四位数字返回年份
			      var month = date.getMonth() + 1;      //月 ,从 Date 对象返回月份 (0 ~ 11) ,date.getMonth()比实际月份少 1 个月
			      var day = date.getDate();             //日 ,从 Date 对象返回一个月中的某一天 (1 ~ 31)
			      var hours = date.getHours();          //小时 ,返回 Date 对象的小时 (0 ~ 23)
			      var minutes = date.getMinutes();      //分钟 ,返回 Date 对象的分钟 (0 ~ 59)
			      var seconds = date.getSeconds();      //秒 ,返回 Date 对象的秒数 (0 ~ 59)
			      //修改月份格式
			      if (month >= 1 && month <= 9) {
			        month = "0" + month;
			      }
			      //修改日期格式
			      if (day >= 0 && day <= 9) {
			        day = "0" + day;
			      }
			      //修改小时格式
			      if (hours >= 0 && hours <= 9) {
			        hours = "0" + hours;
			      }
			      //修改分钟格式
			      if (minutes >= 0 && minutes <= 9) {
			        minutes = "0" + minutes;
			      }
			      //修改秒格式
			      if (seconds >= 0 && seconds <= 9) {
			        seconds = "0" + seconds;
			      }
			      let round = Math.floor(Math.random() * (99 - 10)) + 10;
			      var currentFormatDate = `${year}${month}${day}${hours}${minutes}${seconds}${round}`;
			      return currentFormatDate;
			    },
			backFn(){
				uni.redirectTo({
					url:'/pages/WareHouse/WareHouse'
				})
			},
			// 选择地区域
			customerRegion(e){
				console.log(e);
				this.form.region=e.value[0].text
				this.regionShow=false
			},
			//选择负责人
			selectUser(){
				uni.navigateTo({
					url:"/pages/dialog/user"
				})
			}
		}
	}
</script>

<style lang="scss">
.form{
	   background-color: #fff;
	   padding-left: 10px;
	   padding-right: 10px;
	   
   }
</style>
